<?php

include 'dosen_controller.php';

if ($_GET['action'] == "listDosen") {
    echo viewAllDosen();
} elseif ($_GET['action'] == "account") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewDosenAccount($json);
    } else {
        echo 'Invalid Request';
    }
} elseif ($_GET['action'] == "updateAccount") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        if (updateDosenAccount($json)) {
            echo 'Update Success.';
        } else {
            echo 'Update Failed!';
        }
    } else {        
        $nip = $_POST['nip'];
        $username = $_POST['username'];
        $oldpass = $_POST['oldpass'];
        $newpass = $_POST['newpass'];

        echo updateWebAccount($nip, $username, $oldpass, $newpass);
    }
} elseif ($_GET['action'] == "login") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        $jsArray = json_decode($json, false);
        $nip = $jsArray->{'Nip'};
        $username = $jsArray->{'Username'};
        $password = $jsArray->{'Password'};

        echo loginDosen($nip, $username, $password);
    } else {
        ob_start();
        $nip = $_POST['nip'];
        $password = $_POST['password'];

        echo loginWebAdmin($nip, $password);
    }
} elseif ($_GET['action'] == "approveAll") {
    if (approveAllPendaftaran()) {
        echo 'All registration was approved.';
    } else {
        echo 'Request not completed.';
    }
} elseif ($_GET['action'] == "approveByNim") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        $jsArray = json_decode($json, false);
        $nim = $jsArray->{'Nim'};
        if (approvePendaftaranByNim($nim)) {
            echo 'Registration for Mahasiswa with NIM ' . $nim . ' was approved.';
        } else {
            echo 'Request not completed.';
        }
    } else {
        echo 'No Request send';
    }
} elseif ($_GET['action'] == "rejectByNim") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        $jsArray = json_decode($json, false);
        $nim = $jsArray->{'Nim'};
        if (rejectPendaftaranByNim($nim)) {
            echo 'Registration for Mahasiswa with NIM ' . $nim . ' was rejected.';
        } else {
            echo 'Request not completed.';
        }
    } else {
        echo 'No Request send';
    }
} else {
    echo 'Action not defined!';
}

?>
